package com.maya.medicalwastes;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.maya.medicalwastes.bean.QueryInfoBean;
import com.maya.medicalwastes.entity.QueryNurseInfoBean;
import com.maya.medicalwastes.httpNet.ApiService;
import com.maya.medicalwastes.httpNet.HttpService;
import com.maya.medicalwastes.httpNet.callback.ResponseCallBack;
import com.maya.medicalwastes.httpNet.common.SharedPreferencesConstant;
import com.maya.medicalwastes.httpNet.manage.RetrofitManager;
import com.maya.medicalwastes.httpNet.model.ResultModel;
import com.maya.medicalwastes.ui.OutputActivity;
import com.maya.medicalwastes.util.MyUtils;
import com.maya.medicalwastes.util.PrefUtils;
import com.maya.medicalwastes.util.ToastUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;

/**
 * @author created by fuguoyong
 * 版本：1.0
 * 创建日期：2020-11-19 15
 * 描述：DoctorActivity
 */
public class ForWarderActivity extends AppCompatActivity {

    @BindView(R.id.edt_scan)
    EditText mEditText;
private String id;
private String warehouse;@BindView(R.id.titel)
    TextView titel;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor);
        ButterKnife.bind(this);
        titel.setText("请扫描转运人二维码");
        tv_title.setText("请扫描转运人二维码");
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            id = bundle.getString("ID", "");
            warehouse = bundle.getString("warehouse", "");
        }
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.toString().trim().contains("=")) {
                    if (editable.toString().trim().contains("ZYR")){
                        queryNurseInfo(editable.toString());
                    }else {
                        mEditText.setText("");
                    }

                }
            }
        });
    }


    public void queryNurseInfo(String code) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("codeInformation", code);
        param.put(ParamConstant.TOKEN, PrefUtils.getString(this, SharedPreferencesConstant.TOKEN, ""));
        Observable<ResultModel<QueryInfoBean>> call = RetrofitManager.getInstance().getRetrofit().create(ApiService.class).queryQrcodeInfo(param);
        HttpService.getInstance().enqueue(call, new ResponseCallBack<ResultModel<QueryInfoBean>>() {
            @Override
            public void onResponse(ResultModel<QueryInfoBean> response) {
                if (response != null && response.httpCode == 200) {
                    Bundle bundle = new Bundle();
                    bundle.putString("ID", id);
                    bundle.putString("warehouse", warehouse);
                    bundle.putString("zyr", response.data.getName());
                    bundle.putString("zyrid", response.data.getId());
                    bundle.putString("compName", response.data.getCompName());
                    MyUtils.IntentToOther(ForWarderActivity.this, OutputActivity.class, bundle);
                    finish();
                } else {
                    mEditText.setText("");
                }
            }

            @Override
            public void onFailure(int httpCode) {
                mEditText.setText("");
            }
        }, this);


    }

}
